﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class BezierFunc
{
    #region Vector3的贝塞尔曲线求值信息
    //二阶
    public static Vector3 BezierPoP1(Vector3 p0, Vector3 p1, float t)
    {
        return (1 - t) * p0 + t * p1;
    }
    //三阶
    public static Vector3 BezierP0P1P2(Vector3 p0, Vector3 p1, Vector3 p2,float t)
    {
        return BezierPoP1(BezierPoP1(p0, p1, t), BezierPoP1(p1, p2, t), t);
    }
    //N阶
    public static Vector3 BezierPn(Vector3[] pn, float t, int length)
    {
        if (pn != null && length > 0)
        {
            if (length > 2)
            {
                for (int i = 0; i < length - 1; i++)
                {
                    pn[i] = BezierPoP1(pn[i], pn[i + 1], t);
                }
                return BezierPn(pn, t, length - 1);
            }
            else if (length == 2)
                return BezierPoP1(pn[0], pn[1], t);
            else
                return pn[0];
        }
        else
            return Vector3.zero;
    }
    #endregion
    #region 一维曲线的求值算法
    public static float BezierP0P1(float p0, float p1, float t)
    {
        return (1 - t) * p0 + t * p1;
    }
    public static float BezierPn(float[] pn, float t,int length)
    {
        if (pn != null && length > 0)
        {
            if (length > 2)
            {
                float[] pnnew = new float[pn.Length - 1];
                for (int i = 0; i < pn.Length - 1; i++)
                {
                    pn[i] = BezierP0P1(pn[i], pn[i + 1], t);
                }
                return BezierPn(pn, t, length - 1);
            }
            else if (length == 2)
            {
                return BezierP0P1(pn[0], pn[1], t);
            }
            else
            {
                return pn[0];
            }
        }
        else
            return 0;
    }
    #endregion
}
